<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/controller/ctlAbstract.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busProvider.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUpdate.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busResource.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busComment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busCategory.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busQuestion.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busAddress.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busItem.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/provider.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/update.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/resource.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/comment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/user.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/address.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/stringUtil.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/simpleImage.php';

class ProviderController extends AbstractController {
	private $_providerBus;
	private $_updateBus;
	private $_resourceBus;
	private $_commentBus;
	private $_userBus;
	private $_categoryBus;
	private $_questionBus;
	private $_addressBus;
	
	public function main(array $getVars) {
		global $ubi;
		
		$this->_providerBus = new ProviderBus();
		$this->_updateBus = new UpdateBus();
		$this->_resourceBus = new ResourceBus();
		$this->_commentBus = new CommentBus();
		$this->_userBus = new UserBus();
		$this->_categoryBus = new CategoryBus();
		$this->_questionBus = new QuestionBus();
		$this->_addressBus = new AddressBus();
		
		$userID = isset($_SESSION['userid'])?$_SESSION['userid']:null;
		if ($getVars['action'] == 'favourite') {			
			if ($userID != null) {
				echo $this->_providerBus->favourite($_POST['id'], $userID);
			}
		} else if ($getVars['action'] == 'unfavourite') {
			if ($userID != null) {
				echo $this->_providerBus->removeFavourite($_POST['id'], $userID);
			}
		} else if ($getVars['action'] == 'plus') {
			if ($userID != null) {
				echo $this->_providerBus->plus($_POST['id'], $userID);
			}
		} else if ($getVars['action'] == 'minus') {
			if ($userID != null) {
				echo $this->_providerBus->minus($_POST['id'], $userID);
			}
		} else if ($getVars['action'] == 'uploadImage') {
			$providerID = $getVars['id'];
			$fileName = $_FILES['imgProvider']['name'];
			if (file_exists($ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['RESOURCE_PATH'] . '/' . $fileName)) { 
				$fileName = StringUtil::changeFileName($fileName);
			}
			 
			//create resource model object			
			$res = new ResourceModel();
			$res->setResourceName($fileName);
			$res->setShortDescription('provider');
			
			$id = $this->_resourceBus->addResource($res);
			
			if ($id != 0) {
				$provider = $this->_providerBus->getProvider($providerID);
				
				$numRow = $this->_providerBus->addResource($provider, $id);
				if ($numRow != 0) { 
					$fileSavedResourceName = $ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['RESOURCE_PATH'] . '/' . $fileName;
					$fileSavedThumbnailName = $ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['THUMBNAIL_PATH'] . '/' . $fileName;												
					if (move_uploaded_file($_FILES['imgProvider']['tmp_name'], $fileSavedResourceName)) {
						copy($fileSavedResourceName, $fileSavedThumbnailName);
						
						$image = new SimpleImage();
						$image->load($fileSavedThumbnailName);
						$image->resize($ubi['SITE']['WIDTH_THUMBNAIL'], $ubi['SITE']['HEIGHT_THUMBNAIL']);
						$image->save($fileSavedThumbnailName);

						$image->load($fileSavedResourceName);
						$image->resize($ubi['SITE']['WIDTH_RESOURCE'], $ubi['SITE']['HEIGHT_RESOURCE']);
						$image->save($fileSavedResourceName);
					  	echo '1';	
					  	return;
					}				
				}
			}
			echo '0';			
		} else if ($getVars['action'] == 'loadImage') {
			$providerID = $getVars['id'];
			$providerModel = $this->_providerBus->getProvider($providerID);
			$arrResID = explode(' ', trim($providerModel->getResourceList()));
			foreach ($arrResID as $resID) {
				$arrRes[] = $this->_resourceBus->getResource($resID);
			}
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewImage.php';			
		} else if ($getVars['action'] == 'searchProvider') {
			$text = $_POST['searchProvider'];
			$xml = $this->_providerBus->getSearchProviderByXML($text);			
			echo $xml;
		} else if ($getVars['action'] == 'comment') {
			if ($userID != null) {				
				$commentModel = new CommentModel();
				$commentModel->setInteractType($ubi['PROVIDER']['VALUE']);
				$commentModel->setMessage($_POST['comment']);
				$commentModel->setUserID($_SESSION['userid']);
				$commentModel->setIndicteeID($_POST['id']);
				
				echo $this->_commentBus->addComment($commentModel);					
			}
		} else if ($getVars['action'] == 'loadsocialinfo') {
			$id = $getVars['id'];
			$providerModel = $this->_providerBus->getProvider($id);
			$nComments = $this->_providerBus->getNumberOfComment($id);
			$nFav = $this->_providerBus->getNumberOfFavourites($id);
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewProviderSocialInfo.php';
		} else if ($getVars['action'] == 'loadshortinfo') {
			$id = $getVars['id'];		
							
			$providerModel = $this->_providerBus->getProvider($id);				
			$userModel = $this->_userBus->getUser($providerModel->getUserID());
			$addressModel = $this->_addressBus->getAddress($providerModel->getAddressID());
											
			$arrCatID = explode(' ', trim($providerModel->getCategoryList()));
			
			foreach ($arrCatID as $catID) {
				$catTemp = $this->_categoryBus->getCategoryProviderFromID($catID);
				if ($catTemp) {
					$arrCat[] = $catTemp;
				}
			}
			$this->_questionBus->getQuestionAnswerListFromProvider($id, $arrQuestion, $arrAnswer);
			
			if ($userID != null) {
				if ($this->_providerBus->isFavourite($id, $userID)) {
					$isFavourite = true;						
				}
				if ($this->_providerBus->isMinus($id, $userID)) {
					$isMinus = true;
				}
				if ($this->_providerBus->isPlus($id, $userID)) {
					$isPlus = true;
				}
			}
			
			$itemBus = new ItemBus();
			$itemList = $itemBus->getItemsOfProvider($id);
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewShortInfoProvider.php';			
		} else if ($getVars['action'] == 'edit') {
			$id = $getVars['id'];			
			$providerModel = $this->_providerBus->getProvider($id);
			$strAddress = $this->_addressBus->getAddressString($providerModel->getAddressID());
			$addressModel = $this->_addressBus->getAddress($providerModel->getAddressID());
			
			$arrCatID = split(' ', trim($providerModel->getCategoryList()));			
			
			$arrCat = array();
			$arrCatParent = array();
			foreach ($arrCatID as $catID) {
				$catTemp = $this->_categoryBus->getCategoryProviderFromID($catID);				
				if ($catTemp) {
					$arrCat[] = $catTemp;
					$arrCatParent[] = $this->_categoryBus->getCategoryProviderFromID($catTemp->getParentID());
				}
			}
			$arrQuestion = array();
			$arrAnswer = array();
			
			$this->_questionBus->getQuestionAnswerListFromProvider($id, $arrQuestion, $arrAnswer);
			
			$arrCatProvider1 = $this->_categoryBus->getCategoryProviderList(0);
			$catProvider1 = $arrCatProvider1[0];										
			$arrCatProvider2 = $this->_categoryBus->getCategoryProviderList($catProvider1->getCategoryID());
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewEditProvider.php';
		} else if ($getVars['action'] == 'update') {
			$userID = $_SESSION['userid'];
			if ($userID) {
				$providerModel = $this->_providerBus->getProvider($_POST['providerID']);
				if ($providerModel->getUserID() == $userID) {
					$providerModel->setAnswerList($_POST['answerList']);
					$providerModel->setCategoryList($_POST['categoryProviderList']);
					$providerModel->setCellphone($_POST['txtCellphone']);
					$providerModel->setDescription($_POST['txtProviderDes']);
					$providerModel->setProvider($_POST['txtProviderName']);
					$providerModel->setTelephone($_POST['txtTelephone']);
					$providerModel->setWebsite($_POST['txtWebsite']);
					$providerModel->setProviderID($_POST['providerID']);			
					
					if ($this->_providerBus->updateProvider($providerModel) == 1) {
						$url = 'index.php?view&type=provider&id=' . $providerModel->getProviderID() . '/';
						header( 'Location: ' . $url);
					} else {
						$url = 'index.php?provider&action=edit&id=' . $providerModel->getProviderID() . '/';
						header( 'Location: ' . $url);
					}
				}
			}
		}
	}
}
?>